﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using Ro.AssistFunction.OutputManage;
using Ro.Common.RoArgs;
using Ro.Common.RoInterface.UserType;

namespace Ro.DriverEvent.FindElement
{
    /// <summary>
    /// 构造类
    /// 查找web中控件元素
    /// </summary>
    public class FindWebElement
    {
        /// <summary>
        /// 返回元素
        /// </summary>
        public IWebElement WebElement;

        /// <summary>
        /// 返回xpath值
        /// </summary>
        public string EleXPath;


        /// <summary>
        /// 构造函数
        /// 查询页面中给定的web元素
        /// </summary>
        /// <param name="roWebElementId">Tci+ControlId信息</param>
        /// <param name="timeout">超时信息</param>
        public FindWebElement(string roWebElementId, TimeSpan timeout)
        {
            if (!string.IsNullOrEmpty(roWebElementId))
            {
                //分拆信息，获取tci名称和controlid值
                string[] infos = roWebElementId.Split('.');
                string tciname = infos[0];
                string controlid = infos[1];

                try
                {
                    //获取全部缓存的控件
                    List<RoWeb.ControlUseType> info = RoPubArgments.UseTciInfo.RoWebTci;

                    //todo 新办法 ，通过linq的方式进行查询
                    //通过查找Tci有限返回RoWeb.ControlUseType
                    RoWeb.ControlUseType temptci = (from var in info where var.TciId == tciname select var).SingleOrDefault();

                    if (temptci != null)
                    {
                        RoWeb.OneTciInfo temp = (from onetciinfo in temptci.OneTciInfos where onetciinfo.ControlName == controlid select onetciinfo).SingleOrDefault();

                        if (temp != null)
                        {
                            //返回值
                            EleXPath = temp.Value;
                            WebElement = FindElementInWeb(temp, timeout);
                        }
                        else
                        {
                            //tci 中 id值有错误，或没有对应的id值
                            //添加输出
                            RoPubArgments.RoLog.Fatal("方法FindWebElement已获取TciId，但在查询具体元素时，获取值为空, 请查看ID：" + temptci.TciId);
                            WebElement = null;
                        }
                    }
                    else
                    {
                        //tci id值为空
                        //添加输出
                        RoPubArgments.RoLog.Fatal("方法FindWebElement获取时，TciId获取值为空");
                        WebElement = null;
                    }
                }
                catch (Exception e)
                {
                    ExceptionOutPut.SystemExceptionOut("FindElement", e);
                    WebElement = null;
                }
            }
            else
            {
                //添加输出
                RoPubArgments.RoLog.Fatal("方法FindWebElement获取的roWebElementId参数为空");
                WebElement = null;
            }
        }


        /// <summary>
        /// 私有函数，在页面上查询控件并返回
        /// 等待元素出现，在发现元素
        /// </summary>
        /// <param name="oneTci">RoWeb.ControlInfo信息</param>
        /// <param name="timeout">超时信息</param>
        /// <returns>返回元素</returns>
        private IWebElement FindElementInWeb(RoWeb.OneTciInfo oneTci, TimeSpan timeout)
        {
            //全部预设为null
            IWebElement webElement = null;

            try
            {
                //如果载入的参数不为空
                if (oneTci != null)
                {
                    //todo 4-18 很多控件都存在若干个，没办法，接着把index值添加进去
                    ReadOnlyCollection<IWebElement> eles = null;
                    //设置查找时间秒
                    RoTestObject.WebTestObject.Manage().Timeouts().ImplicitWait = timeout;
                    //设置等待
                    WebDriverWait wait = new WebDriverWait(RoTestObject.WebTestObject, timeout);

                    switch (oneTci.Locator)
                    {
                        case "Class":
                            //wait.Until(ExpectedConditions.ElementExists(By.ClassName(oneTci.Value)));
                            eles = RoTestObject.WebTestObject.FindElements(By.ClassName(oneTci.Value));
                            break;

                        case "Css":
                            //wait.Until(ExpectedConditions.ElementExists(By.CssSelector(oneTci.Value)));
                            eles = RoTestObject.WebTestObject.FindElements(By.CssSelector(oneTci.Value));
                            break;

                        case "Id":
                            //wait.Until(ExpectedConditions.ElementExists(By.Id(oneTci.Value)));
                            eles = RoTestObject.WebTestObject.FindElements(By.Id(oneTci.Value));
                            break;

                        case "Name":
                            //wait.Until(ExpectedConditions.ElementExists(By.Name(oneTci.Value)));
                            eles = RoTestObject.WebTestObject.FindElements(By.Name(oneTci.Value));
                            break;

                        case "XPath":
                            //wait.Until(ExpectedConditions.ElementExists(By.XPath(oneTci.Value)));
                            eles = RoTestObject.WebTestObject.FindElements(By.XPath(oneTci.Value));
                            break;

                        case "Link":
                            //wait.Until(ExpectedConditions.ElementExists(By.LinkText(oneTci.Value)));
                            eles = RoTestObject.WebTestObject.FindElements(By.LinkText(oneTci.Value));
                            break;


                           
                    }
                    if (eles != null)
                    {
                        if (eles.Count==1)
                        {
                            webElement = eles[0];
                        }
                        else
                        {
                            int index = oneTci.Index - 1;
                            webElement = eles[index];
                        }
                        

                        //RoPubArgments.RoLog.Debug("查询到的控件数量:" + eles.Count);
                    }
                    else
                    {
                        //添加log
                        RoPubArgments.RoLog.Fatal("FindElementInWeb查找控件已完成,但无法获取正确控件\r\n控件Value为：" + oneTci.Value);
                    }
                }
                return webElement;
            }


            
            catch (Exception e)
            {
                ExceptionOutPut.SystemExceptionOut("FindWebElement", e);
                return null;
            }
        }
    }
}
